package com.ljh.action;

import com.google.gson.Gson;
import com.ljh.pojo.Books;
import com.ljh.pojo.User;
import com.ljh.service.BookService;
import com.ljh.service.UserService;
import com.ljh.util.FileTraverse;
import com.ljh.util.FileUpload;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;


public class BookAction extends ActionSupport {
    private String ajax;

    public String getAjax() {
        return ajax;
    }

    public void setAjax(String ajax) {
        this.ajax = ajax;
    }

    private Integer bookID;
    private String bookName;
    private Integer bookCounts;
    private String detail;
    private String id;
    private String password;
    private File file;
    private String fileFileName;
    private String url;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    public String getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }

    private String fileContentType;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


    public Integer getBookID() {
        return bookID;
    }

    public void setBookID(Integer bookID) {
        this.bookID = bookID;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public Integer getBookCounts() {
        return bookCounts;
    }

    public void setBookCounts(Integer bookCounts) {
        this.bookCounts = bookCounts;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;

    public String list(){
        List<Books> list=bookService.queryAllBook();
        ActionContext.getContext().getValueStack().set("list",list);
        return "admin/allBook";
    }

    public String toAddBook(){
        return "admin/toAddBook";
    }
    public String addBook() throws IOException {
        FileUpload fileUpload=new FileUpload();
        String url=fileUpload.fileUpload(this.fileFileName,this.file);
        Books books=new Books(this.bookID,this.bookName,this.bookCounts,this.detail,url);
        bookService.addBook(books);
        return "book/allBook";
    }
    public String toUpdateBook(){
        FileTraverse fileTraverse=new FileTraverse();
        List<String> imageUrl = fileTraverse.fileTraverse();
        ActionContext.getContext().getValueStack().set("imageUrl",imageUrl);
        Books book=bookService.queryBookById(this.bookID);
        ActionContext.getContext().getValueStack().set("book",book);
        return "admin/toUpdateBook";
    }
    public String UpdateBook() throws IOException {
        if(this.url==null||this.url.equals("")){
            FileUpload fileUpload=new FileUpload();
            String url=fileUpload.fileUpload(this.fileFileName,this.file);
            Books books=new Books(this.bookID,this.bookName,this.bookCounts,this.detail,url);
            bookService.updateBook(books);
            return "book/allBook";
        }else {
            Books books=new Books(this.bookID,this.bookName,this.bookCounts,this.detail,this.url);
            bookService.updateBook(books);
            return "book/allBook";
        }
    }
    public String del(){
        bookService.deleteBookById(this.bookID);
        return "book/allBook";
    }
    public String query(){
        List<Books> list=bookService.queryBookByName(this.bookName);
        ActionContext.getContext().getValueStack().set("list",list);
        if(list.size()<1)
        ActionContext.getContext().getValueStack().set("msg","未查到");
        return "admin/allBook";
    }
    public String toLogin(){
        return "login";
    }
    public String toRegister(){
        return "register";
    }
    public String search_box(){
        List<Books> list=bookService.queryBookByName(this.bookName);
        Gson gson=new Gson();
        this.ajax=gson.toJson(list);
        return "success";
    }
    public String login(){
        User user=new User(this.id,this.password);
        User user1=userService.queryUser(user);
        if(userService.queryUser(user)!=null){
            ActionContext.getContext().getSession().put("user",user1.getId());
            if(user1.getId().equals("admin")){
                return "book/allBook";
            }else{
                return "userbook/user";
            }
        }else{
            return "login";
        }
    }
    public String register(){
        User user=new User(this.id,this.password);
            userService.addUesr(user);
            return "login";
    }
//    @RequestMapping(value = "ReId",produces = "text/html;charset=UTF-8")
//    @ResponseBody
    public String ReId(){
        Gson gson=new Gson();
        User user= null;
        try {
            user = userService.queryUserById(this.id);
        } catch (Exception e) {
            this.ajax=gson.toJson(true);
            return "true";
        }
        if(user==null){
            this.ajax=gson.toJson(true);
            return "true";
        }else{
            this.ajax=gson.toJson(false);
            return "false";
        }
    }
}
